package com.yonyou.pmclouds.constructiondiary.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.DocCenterProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.ProjectStatusCheckProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.constructiondiary.entity.ConstructionDiaryVO;
import com.yonyou.pmclouds.constructiondiary.mapper.ConstructionDiaryMapper;
import com.yonyou.pmclouds.constructiondiary.process.ConstructionDiaryInsertCheckProcessor;
import com.yonyou.pmclouds.constructiondiary.rmiitf.ConstructionDiaryMaintain;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.project.processor.ProjectBillUpdateForUpdate;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForDeleteProcessor;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForInsertProcessor;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import com.yonyou.pmclouds.workflow.service.rmiitf.IFlowApproveBuziService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass= ConstructionDiaryMaintain.class)
public class ConstructionDiaryMaintainImpl implements ConstructionDiaryMaintain, IFlowApproveBuziService {

    @Autowired
    private ConstructionDiaryMapper mapper;
    @Autowired
    private PictureRemoteService pictureService;
    @Autowired
    private UpdateBillCountForInsertProcessor.UpdateBillCountForInsertProcessorFactory<ConstructionDiaryVO> billCountFactory;
    @Autowired
    private ProjectStatusCheckProcessor<ConstructionDiaryVO> projectStatusCheckProcessor;
    @Autowired
    private ProjectBillUpdateForUpdate.ProjectBillForUpdateProcessorFactory<ConstructionDiaryVO> billForUpdateProcessorFactory;
    @Autowired
    private UpdateBillCountForDeleteProcessor.UpdateBillCountForDeleteProcessorFactory<ConstructionDiaryVO> billCountDeleteFactory;
    @Autowired
    private DocCenterProcessor docCenterProcessor;

    private static String[] UPDATE_STATUS = {ConstructionDiaryVO.BILLSTATUS};

    @Override
    @PMApprove
    public ConstructionDiaryVO insert(ConstructionDiaryVO constructionDiaryVO) throws BusinessException {

        SingleInsertBpTemplate<ConstructionDiaryVO> bp = new SingleInsertBpTemplate<ConstructionDiaryVO>(mapper);
        //更新审计信息
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<ConstructionDiaryVO>());
        //保存前校验
        bp.addBeforeProcessor(new ConstructionDiaryInsertCheckProcessor());

        bp.addBeforeProcessor(projectStatusCheckProcessor);

        bp.addAfterProcessor(billCountFactory.getProcessor(BillTypeConst.CONSTRUCTION_DIARY_BILLTYPE));
        //状态设置为审批中
        constructionDiaryVO.setBillstatus(BillStatusConst.COMMON_FREE_STATUS);
        bp.insert(constructionDiaryVO);
        //保存图片
        pictureService.insertPictureForPub(constructionDiaryVO, null);
        return constructionDiaryVO;
    }

    @Override
    public ConstructionDiaryVO approve(String pkConstructiondiary, byte billStatus) throws BusinessException {
        if(StringUtils.isEmpty(pkConstructiondiary)){
            return null;
        }
        ConstructionDiaryVO constructionDiaryVO = mapper.getById(pkConstructiondiary);
        SingleUpdateBpTemplate<ConstructionDiaryVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);

        constructionDiaryVO.setBillstatus(billStatus);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.CONSTRUCTION_DIARY_BILLTYPE));
        bp.addAfterProcessor(docCenterProcessor);
        bp.update(constructionDiaryVO);

        return constructionDiaryVO;
    }

    @Override
    public ConstructionDiaryVO reject(String pkConstructiondiary) throws BusinessException {
        if(StringUtils.isEmpty(pkConstructiondiary)){
            return null;
        }
        ConstructionDiaryVO constructionDiaryVO = mapper.getById(pkConstructiondiary);
        SingleUpdateBpTemplate<ConstructionDiaryVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);
        constructionDiaryVO.setBillstatus(BillStatusConst.COMMON_REJECT_STATUS);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.CONSTRUCTION_DIARY_BILLTYPE));
        bp.update(constructionDiaryVO);
        return constructionDiaryVO;
    }

    @Override
    public SuperVO approveByPkBill(String pk_bill, byte billStatus) throws BusinessException {
        return approve(pk_bill, billStatus);
    }

    @Override
    public SuperVO rejectByPkBill(String pk_bill) throws BusinessException {
        return reject(pk_bill);
    }

    @Override
    public SuperVO queryDetailForWorkFlow(String pk_bill) throws BusinessException {
        ConstructionDiaryVO constructionDiaryVO = mapper.getById(pk_bill);
        return constructionDiaryVO;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.CONSTRUCTION_DIARY_BILLTYPE;
    }

    @Override
    public SuperVO deleteBill(SuperVO businessVO) throws BusinessException {
        SingleDeleteBpTemplate<ConstructionDiaryVO> deleteBpTemplate = new SingleDeleteBpTemplate<>(mapper);
        deleteBpTemplate.addAfterProcessor(billCountDeleteFactory.getProcessor(BillTypeConst.CONSTRUCTION_DIARY_BILLTYPE));
        return deleteBpTemplate.delete((ConstructionDiaryVO) businessVO);
    }

    @Override
    public Object getMapper() {
        return mapper;
    }
}
